package com.corner.presenter.imp;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.corner.presenter.ISeachLocationPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.ISearchLocationView;
import com.corner.utils.LocationUtil;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/24，10.09.
 * 描述：
 */
public class SearchLocationPresenter extends BasePresenter<ISearchLocationView> implements ISeachLocationPresenter {

    private boolean hasMore = true;
    private boolean isLoading = false;
    private int currentPage = 0;
    private List<PoiItem> items;
    private String keyword;

    @Override
    public void requestLocation(final String keyword, final Context context) {
        if (keyword.length() < 2) {
            return;
        }
        this.keyword = keyword;
        if (isLoading) {
            return;
        }
        isLoading = true;
        LocationUtil.getInstance().startLocation();
        LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
            @Override
            public void onLocationGet(final AMapLocation location) {
                Observable.create(new Observable.OnSubscribe<List<PoiItem>>() {
                    @Override
                    public void call(Subscriber<? super List<PoiItem>> subscriber) {
                        PoiSearch.Query query = new PoiSearch.Query(keyword, "", "");
                        query.setPageSize(10);// 设置每页最多返回多少条poiitem
                        currentPage = 0;
                        query.setPageNum(currentPage);//设置查第一页
                        PoiSearch poiSearch = new PoiSearch(context, query);
                        try {
                            //开始搜索
                            PoiResult result = poiSearch.searchPOI();
                            currentPage++;
                            List<PoiItem> items = result.getPois();
                            if (items.size() < 10) {
                                hasMore = false;
                            }
                            SearchLocationPresenter.this.items = items;
                            subscriber.onNext(SearchLocationPresenter.this.items);
                        } catch (AMapException e) {
                            e.printStackTrace();
                            subscriber.onError(new Throwable());
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<PoiItem>>() {
                    @Override
                    public void call(List<PoiItem> poiItems) {
                        if (view != null) {
                            view.showPois(poiItems);
                            view.setLoadMoreEnable(hasMore);
                        }
                        isLoading = false;
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        isLoading = false;
                    }
                });
            }
        });
    }

    @Override
    public void loadMoreLocation(final Context context) {
        if (!hasMore || isLoading) {
            return;
        }
        isLoading = true;
        LocationUtil.getInstance().startLocation();
        LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
            @Override
            public void onLocationGet(final AMapLocation location) {
                Observable.create(new Observable.OnSubscribe<List<PoiItem>>() {
                    @Override
                    public void call(Subscriber<? super List<PoiItem>> subscriber) {
                        PoiSearch.Query query = new PoiSearch.Query(keyword, "", "");
                        query.setPageSize(10);// 设置每页最多返回多少条poiitem
                        query.setPageNum(currentPage);//设置查第一页
                        PoiSearch poiSearch = new PoiSearch(context, query);
                        try {
                            //开始搜索
                            PoiResult result = poiSearch.searchPOI();
                            currentPage++;
                            List<PoiItem> items = result.getPois();
                            if (items.size() < 10) {
                                hasMore = false;
                            }
                            if (SearchLocationPresenter.this.items == null) {
                                SearchLocationPresenter.this.items = items;
                            } else {
                                items.addAll(0, SearchLocationPresenter.this.items);
                                SearchLocationPresenter.this.items = items;
                            }
                            subscriber.onNext(SearchLocationPresenter.this.items);
                        } catch (AMapException e) {
                            e.printStackTrace();
                            subscriber.onError(new Throwable());
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<PoiItem>>() {
                    @Override
                    public void call(List<PoiItem> poiItems) {
                        if (view != null) {
                            view.showPois(poiItems);
                            view.setLoadMoreEnable(hasMore);
                        }
                        isLoading = false;
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        isLoading = false;
                    }
                });
            }
        });
    }
}
